package com.zidian.leader.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.zidian.leader.R;
import com.zidian.leader.adapter.MainItemTypeAdapter;
import com.zidian.leader.api.Constant;
import com.zidian.leader.common.activity.BaseActivity;
import com.zidian.leader.common.util.HawkUtils;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import rx.subscriptions.Subscriptions;

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {

    @Bind(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @Bind(R.id.nav_view)
    NavigationView navView;
    @Bind(R.id.main_menu_rv)
    RecyclerView mainMenuRv;

    private MainItemTypeAdapter adapter;
    private PopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        adapter.unSubscribeView();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    /**
     * 初始化视图
     */
    private void initView() {
        //Toolbar
        actionBar.setTitle(R.string.main_title);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                    drawerLayout.closeDrawer(Gravity.LEFT);
                } else {
                    drawerLayout.openDrawer(GravityCompat.START);
                }
            }
        });

        navView.setNavigationItemSelectedListener(this);
        View navHeaderView = navView.getHeaderView(0);
        TextView navTitle = (TextView) navHeaderView.findViewById(R.id.nav_title_tv);
        TextView navName = (TextView) navHeaderView.findViewById(R.id.nav_name_tv);
        navName.setText(HawkUtils.getLeadName());
        if (HawkUtils.getRoleId() == 5) {
            navTitle.setText(R.string.user_info_headmaster);
        } else {
            navTitle.setText(R.string.user_info_dean);
        }
        adapter = new MainItemTypeAdapter();
        mainMenuRv.setLayoutManager(new GridLayoutManager(this, 3));
        mainMenuRv.setAdapter(adapter);

        //页面跳转
        adapter.setOnItemClickListener(new MainItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String type) {
                if (Constant.MenuItemType.STUDENT_STATISTICS.equals(type)) {
                    startActivity(StudentStatisticsActivity.class);
                } else if (Constant.MenuItemType.TEACHING_QUALITY.equals(type)) {
                    startActivity(TeachingQualityActivity.class);
                } else if (Constant.MenuItemType.ENROL_ADAPTATION.equals(type)) {
                    startActivity(EnrolAdaptationActivity.class);
                } else if (Constant.MenuItemType.EMPLOYMENT_QUALITY.equals(type)) {
                    startActivity(EmploymentQualityActivity.class);
                } else {
                    Intent intent = new Intent(MainActivity.this, IntroductionActivity.class);
                    intent.putExtra("title", type);
                    startActivity(intent);
                }
            }
        });
    }

    /**
     * 侧滑菜单Item点击事件
     *
     * @param item
     * @return
     */
    @Override
    public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
        drawerLayout.closeDrawer(GravityCompat.START);
        drawerLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                int id = item.getItemId();
                if (id == R.id.nav_password) {
                    startActivity(ChangePasswordActivity.class);
                } else if (id == R.id.nav_exit) {
                    showPopupWindow();
                } else if (id == R.id.nav_about) {
                    startActivity(AboutActivity.class);
                }
            }
        }, 250);
        return true;
    }

    /**
     * 显示退出popupWindow
     */
    private void showPopupWindow() {
        if (popupWindow == null) {
            TextView drawerLayoutPopupWindowSureTV;
            TextView drawerLayoutPopupWindowCanCelTV;
            View view = LayoutInflater.from(this).inflate(R.layout.logout_popupwindow, null, false);
            drawerLayoutPopupWindowSureTV = (TextView) view.findViewById(R.id.drawerLayoutPopupWindowSureTV);
            drawerLayoutPopupWindowCanCelTV = (TextView) view.findViewById(R.id.drawerLayoutPopupWindowCanCelTV);
            drawerLayoutPopupWindowCanCelTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                }
            });
            drawerLayoutPopupWindowSureTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(LoginActivity.class);
                    HawkUtils.clearPreferences();
                }
            });
            popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        }

        popupWindow.showAtLocation(drawerLayout, Gravity.CENTER, 0, 0);
    }

    private boolean isAppExit = false;

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);

        } else {
            if (!isAppExit) {
                isAppExit = true;
                Snackbar.make(drawerLayout, "再按一次退出程序", Snackbar.LENGTH_SHORT).show();
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        isAppExit = false;
                    }
                }, 2000);
            } else {
                super.onBackPressed();
            }
        }
    }

}
